<!-- 一期一档 -->
<template>
  <div class="one_stage_one_gear">
    <div class="search">
      <el-form ref="form" :model="queryParams.params" label-width="0">
        <el-form-item>
          <el-input
            v-model="queryParams.params.className"
            placeholder="培训班名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="search"
          >查询</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="InformationFilling('add')"
          >添加</el-button
        >
      </div>
    </div>
    <div class="table">
      <el-table
        height="100%"
        :data="tableData"
        :header-cell-style="{
          background: '#f2f3f4',
          color: '#555',
          'font-weight': 'bold',
          'line-height': '32px',
        }"
        border
        style="width: 100%; height: 100%; overflow: auto"
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="className" label="培训班名称"> </el-table-column>
        <el-table-column prop="headTeacherName" label="班主任" width="180">
        </el-table-column>
        <el-table-column prop="trainingMode" label="培训方式" width="180">
        </el-table-column>
        <el-table-column prop="trainingMode" label="档案状态" width="180">
          <template slot-scope="scope">
            <template v-if="scope.row.exportStatus == 0"> 未生成 </template>
            <template v-if="scope.row.exportStatus == 1"> 生成中 </template>
            <template v-if="scope.row.exportStatus == 2"> 已生成 </template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="460">
          <template slot-scope="scope">
            <!-- <el-button
              type="primary"
              size="small"
              icon="el-icon-view"
              @click="InformationFilling('look', scope.row)"
              >查看</el-button
            >
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="InformationFilling('update', scope.row)"
              >修改</el-button
            > -->

            <el-button
              type="primary"
              size="small"
              @click="FileManagement(scope.row)"
              ><i class="iconfont icon-dangan"></i> 档案管理</el-button
            >
            <el-button type="primary" size="small" @click="derive(scope.row, 1)"
              ><i class="iconfont icon-daochu"></i> 生成档案</el-button
            >
            <el-button
              :disabled="scope.row.exportStatus != 2"
              type="primary"
              size="small"
              @click="derive(scope.row, 2)"
              ><i class="iconfont icon-daochu"></i> 下载档案</el-button
            >
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              @confirm="InformationDel(scope.row)"
              title="确定删除吗？"
            >
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                style="margin-left: 10px"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="queryParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      title="档案管理"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <div class="archivesList">
        <div v-for="(item, index) in archivesList" :key="index" class="item">
          <div class="itemContent" @click="archivesManage(item)">
            <div class="icon">
              <i :class="item.icon"></i>
            </div>
            <div class="text">
              <span>{{ item.name }}</span>
            </div>
          </div>
        </div>
        <div style="width: 30%"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  fetchDetail,
  exportArchives,
} from "@/api/OneStageOneGear/OneStageOneGear.js";
export default {
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "添加信息",
      tableData: [],
      archivesRadio: "",
      archivesList: [
        {
          path: "pxbInformation",
          name: "培训班基础信息",
          icon: "iconfont icon-peixunbanxinxihuizong",
        },
        {
          path: "pxbCourse",
          name: "培训班课程安排",
          icon: "iconfont icon-kechenganpai",
        },
        {
          path: "CourseMaterial",
          name: "课程教材与电子讲义",
          icon: "iconfont icon-kechengjiaocai",
        },
        {
          path: "xyInformation",
          name: "学员基础信息",
          icon: "iconfont icon-xueyuanxinxi",
        },
        {
          path: "xyClockingIn",
          name: "学员考勤",
          icon: "iconfont icon-xueyuankaoqin",
        },
        {
          path: "bzrWorkLog",
          name: "班主任工作日志",
          icon: "iconfont icon-gongzuorizhi",
        },
        {
          path: "TeachingPlanExecution",
          name: "教学计划执行",
          icon: "iconfont icon-jiaoxuejihua",
        },
        { path: "SignIn", name: "授课教师签到", icon: "iconfont icon-qiandao" },
        {
          path: "SatisfactionEvaluation",
          name: "满意度测评",
          icon: "iconfont icon-manyidu",
        },
        {
          path: "ExaminationSituation",
          name: "学员考核情况",
          icon: "iconfont icon-kaoshiqingkuang",
        },
        {
          path: "EvaluationReport",
          name: "综合考评报告",
          icon: "iconfont icon-kaopingbaogao",
        },
        {
          path: "TrainingAgreement",
          name: "委托培训协议",
          icon: "iconfont icon-peixunxieyi",
        },
      ],
      total: 0,
      queryParams: {
        current: 1,
        params: {
          className: "",
        },
        size: 10,
      },
    };
  },
  created() {
    this.getDetail();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    },
    async getDetail() {
      const res = await fetchList(this.queryParams);
      console.log(res);
      this.tableData = res.data.records;
      this.total = res.data.total;
    },
    derive(record, type) {
      switch (type) {
        case 1:
          exportArchives(record.id).then((res) => {
            if (res.code == 200) {
              this.$alert(res.data, "提示", {
                confirmButtonText: "确定",
                type: "success",
                callback: (action) => {},
              });
            }
          });
          break;
        case 2:
          let a = document.createElement("a"); //创建一个<a></a>标签
          a.href = location.origin + record.exportUrl; //给a标签的href属性值加上地址，注意，这里是绝对路径，不用    加 点.
          a.download = record.className + ".docx"; //设置下载文件文件名，这里加上.docx指定文件类型，pdf文件就指定.pdf即可
          a.style.display = "none"; //隐藏a标签
          document.body.append("a"); //将a标签追加到document.body中
          a.click(); //模拟点击a标签，会触发a标签的href的读取，浏览器就会自动下载了
          a.remove(); // 删除a标签（一次性的）

          break;
        default:
          break;
      }

      // this.$message("导出");
    },
    FileManagement(row) {
      this.dialogVisible = true;
      this.archivesRadio = row.id;
    },
    archivesManage(item) {
      if (item.path == "pxbInformation") {
        this.$router.push({
          name: item.path,
          query: {
            Type: "202501012025013",
            Id: this.archivesRadio,
          },
        });
      } else {
        this.$router.push({
          name: item.path,
          query: { id: this.archivesRadio },
        });
      }
    },
    async InformationDel(row) {
      console.log(row);
      let res = await fetchDetail(row.id);
      console.log(res);
      if (res.code === 200) {
        this.$message({ message: "删除成功", type: "success" });
        this.getDetail();
      } else {
        this.$message({ message: "删除失败", type: "error" });
      }
    },
    InformationFilling(type, row) {
      let id = "";
      let satrt = ""; //20250101202501添加信息2025010120250102查看信息202501012025013修改信息
      if (type === "add") {
        satrt = "20250101202501";
      } else if (type === "look") {
        satrt = "2025010120250102";
        id = row.id;
      } else if (type === "update") {
        satrt = "202501012025013";
        id = row.id;
      }
      this.$router.push({
        name: "pxbInformation",
        query: { Type: satrt, Id: id },
      });
    },
    handleClose(done) {
      done();
    },
    search() {
      this.getDetail();
    },
    handleSizeChange(val) {
      this.queryParams.size = val;
      this.getDetail();
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getDetail();
    },
  },
};
</script>

<style lang="less" scoped>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.one_stage_one_gear {
  height: calc(100vh - 100px);
  padding: 20px;

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-form-item {
      margin-bottom: 0;
    }
  }

  .table {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    height: calc(100% - 50px);

    .pagination {
      margin-top: 20px;
      text-align: right;
      margin-right: 5px;
    }

    .iconfont {
      font-size: 12px;
    }
  }
}

.archivesList {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .item {
    width: 30%;
    border: 3px solid;
    border-radius: 5px;
    text-align: center;
    margin-bottom: 5px;
    cursor: pointer;

    .itemContent {
      display: flex;
      align-items: center;
      justify-content: space-around;
      padding: 20px;

      .icon {
        width: 20%;
      }

      .text {
        width: 80%;
      }
    }
  }

  .iconfont {
    font-size: 30px;
  }
}
</style>
